﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Xml;
using System.Net;

namespace eBay {
    public partial class API {

        public class FindingAPI {

            public static GetSearchKeywordsRecommendationResponseType GetSearchKeywordsRecommendation(string keywords) {
                //Load the XML Document to Use for this Request
                XmlDocument xmlDoc = new XmlDocument();

                //Get XML Document from Embedded  Resources
                xmlDoc.Load(Common.Funcs.GetAppPath() + "APICalls\\GetSearchKeywordsRecommendation.xml");
                XmlNode nd = xmlDoc["getSearchKeywordsRecommendationRequest"];

                nd["keywords"].InnerText = keywords;

                //Get XML into a string for use in encoding
                string xmlText = xmlDoc.InnerXml;


                //Put the data into a UTF8 encoded  byte array
                UTF8Encoding encoding = new UTF8Encoding();
                int dataLen = encoding.GetByteCount(xmlText);
                byte[] utf8Bytes = new byte[dataLen];
                Encoding.UTF8.GetBytes(xmlText, 0, xmlText.Length, utf8Bytes, 0);

                HttpWebRequest request = eBayRequest.SetupRequest("getSearchKeywordsRecommendation", utf8Bytes.Length, -1, -1, Global.Configuration.FindingAPIUrl, APITypes.FindingAPI);
                return new GetSearchKeywordsRecommendationResponseType(eBayRequest.SendRequest(request, utf8Bytes));
            }

            public static FindItemsByCategoryResponseType FindItemsByCategory(string categoryID) {
                //Load the XML Document to Use for this Request
                XmlDocument xmlDoc = new XmlDocument();

                //Get XML Document from Embedded  Resources
                xmlDoc.Load(Common.Funcs.GetAppPath() + "APICalls\\FindItemsByCategoryRequest.xml");
                XmlNode nd = xmlDoc["findItemsByCategoryRequest"];

                nd["categoryId"].InnerText = categoryID;

                //Get XML into a string for use in encoding
                string xmlText = xmlDoc.InnerXml;


                //Put the data into a UTF8 encoded  byte array
                UTF8Encoding encoding = new UTF8Encoding();
                int dataLen = encoding.GetByteCount(xmlText);
                byte[] utf8Bytes = new byte[dataLen];
                Encoding.UTF8.GetBytes(xmlText, 0, xmlText.Length, utf8Bytes, 0);

                HttpWebRequest request = eBayRequest.SetupRequest("findItemsByCategory", utf8Bytes.Length, -1, -1, Global.Configuration.FindingAPIUrl, APITypes.FindingAPI);
                return new FindItemsByCategoryResponseType(eBayRequest.SendRequest(request, utf8Bytes));
            }
        }
    }
}
